<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "dmd_art_teaching".
 *
 * @property integer $id
 * @property string $title
 * @property integer $type
 * @property string $price
 * @property integer $class_count
 * @property integer $created_at
 * @property integer $teacher
 * @property integer $course
 * @property integer $classroom
 * @property integer $mechanic_id
 * @property integer $class_day
 * @property integer $status
 * @property string $class_time
 * @property integer $max_count
 * @property string $star_time
 * @property string $end_time
 */
class ArtTeaching extends \zhtfrontend\components\bases\BaseModel
{
    public $teacher;
    public $course;
    public $classroom;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%art_teaching}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'class_count','class_day','max_count'], 'integer'],
            [['price'], 'number'],
            [['title'], 'string', 'max' => 50],
            [['star_time','end_time'], 'string', 'max' => 10],
            [['star_time', 'end_time',], 'check_time'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'title' => Yii::t('zhtfrontend', '标题'),
            'type' => Yii::t('zhtfrontend', '类型'),
            'price' => Yii::t('zhtfrontend', '价格'),
            'class_count' => Yii::t('zhtfrontend', '课次'),
            'created_at' => Yii::t('zhtfrontend', '添加时间'),
            'max_count' => Yii::t('zhtfrontend', '最大人数'),
            'star_time' => Yii::t('zhtfrontend', '开始时间'),
            'end_time' => Yii::t('zhtfrontend', '结束时间'),
            'teacher' => Yii::t('zhtfrontend', '老师'),
            'course' => Yii::t('zhtfrontend', '课程'),
            'classroom' => Yii::t('zhtfrontend', '教室'),
            'mechainc_id' => Yii::t('zhtfrontend', '机构id'),
            'class_day' => Yii::t('zhtfrontend', '上课日期'),
            'status' => Yii::t('zhtfrontend', '状态'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['type'] = function () {
            switch ($this->type) {
                case 1:
                    return \Yii::t('zhtfrontend', '益智');
                case 2:
                    return \Yii::t('zhtfrontend', '音乐');
                case 3:
                    return \Yii::t('zhtfrontend', '形体');
            }
        };
        $fields['class_day'] = function () {
            switch ($this->class_day) {
                case 1:
                    return \Yii::t('zhtfrontend', '星期一');
                case 2:
                    return \Yii::t('zhtfrontend', '星期二');
                case 3:
                    return \Yii::t('zhtfrontend', '星期三');
                case 4:
                    return \Yii::t('zhtfrontend', '星期四');
                case 5:
                    return \Yii::t('zhtfrontend', '星期五');
                case 6:
                    return \Yii::t('zhtfrontend', '星期六');
                case 7:
                    return \Yii::t('zhtfrontend', '星期七');
            }
        };

        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'delete' => function ($data) {
                            $group = GoodsGroup::find()->where(['group_id'=>$this->id])->one();
                            if ($group != null) {
                                return '<button class="yx-btn-disabled" disabled>删除</button>';
                            } else {
                                return '';
                            }
                        },
                        'update' => function ($data) {
                           return '';
                        },
                        'view' => function(){
                            return '';
                        }
                    ]
                )->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    //ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],

        ]);
    }

    public function check_time($attribute){
        if(!empty($this->star_time) && !empty($this->end_time) ){
            $start_time = (int)str_replace(':', '', $this->star_time);
            $end_time = (int)str_replace(':', '', $this->end_time);
            if ($end_time <= $start_time) {
                $this->addError($attribute, Yii::t('zhtfrontend', '下课时间小于等于上课时间！'));
            }
        }
    }

    public function getMechanic(){
        return $this->hasOne(Mechanic::className(),['id'=>'mechanic_id']);
    }

    public function afterDelete()
    {
        parent::afterDelete(); // TODO: Change the autogenerated stub
        $art_relation = ArtRelation::find()->select('arrangement_id')->where(['art_teaching_id'=>$this->id])->asArray()->column();
        if($art_relation){
            Arrangement::deleteAll(['id' =>$art_relation]);
            ArtRelation::deleteAll('art_teaching_id=:id',[':id'=>$this->id]);
        }

    }


}
